package com.keyroy.game.puzzle_rpg.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManageX;

import com.keyroy.game.puzzle_rpg.models.interfaces.EventListener;
import com.keyroy.game.puzzle_rpg.models.puzzle.TileType;
import com.keyroy.game.puzzle_rpg.utils.FileUtil;
import com.keyroy.game.puzzle_rpg.views.IconView;

public class TileTypeEditor extends Shell {
	private Text text;
	private Text imagePath;
	private IconView canvas;
	private int color;

	private TileType tileType;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			TileTypeEditor shell = new TileTypeEditor(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public TileTypeEditor(Display display, TileType tileType, final EventListener<TileType> listener) {
		this(display, listener);
		this.tileType = tileType;
		set(tileType);
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 * @wbp.parser.constructor
	 */
	public TileTypeEditor(Display display, final EventListener<TileType> listener) {
		super(display, SWT.DIALOG_TRIM);

		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setBounds(10, 10, 35, 17);
		lblNewLabel.setText("\u540D\u79F0 : ");

		text = new Text(this, SWT.BORDER);
		text.setBounds(51, 7, 383, 23);

		Label lblNewLabel_1 = new Label(this, SWT.NONE);
		lblNewLabel_1.setBounds(10, 39, 35, 17);
		lblNewLabel_1.setText("\u56FE\u6807 : ");

		imagePath = new Text(this, SWT.BORDER);
		imagePath.setEditable(false);
		imagePath.setBounds(51, 36, 297, 23);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String path = FileUtil.selectFile(getShell(), null, null, new String[] { "*.png", "*.gif" });
				if (path != null) {
					imagePath.setText(path);
				}

				canvas.setImage(path);
			}
		});
		btnNewButton.setBounds(354, 34, 80, 27);
		btnNewButton.setText("\u9009\u62E9\u6587\u4EF6");

		canvas = new IconView(this);
		canvas.setBounds(10, 65, 424, 164);

		Button btnNewButton_1 = new Button(this, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (listener != null) {
					TileType tileType = new TileType();
					if (text.getText().length() > 0) {
						tileType.name = text.getText();
					}
					if (imagePath.getText().length() > 0) {
						tileType.icon = imagePath.getText();
					}
					if (color != 0) {
						tileType.color = color;
					}
					listener.onEvent(tileType);
					dispose();
				}
			}
		});
		btnNewButton_1.setBounds(354, 235, 80, 27);
		btnNewButton_1.setText("\u5B8C\u6210");

		Button btnNewButton_2 = new Button(this, SWT.NONE);
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ColorDialog dlg = new ColorDialog(getShell());
				RGB rgb = dlg.open();
				if (rgb != null) {
					canvas.setBackground(ResourceManageX.getColor(rgb));
					color = ResourceManageX.getRGB(rgb);
				}
			}
		});
		btnNewButton_2.setBounds(10, 235, 80, 27);
		btnNewButton_2.setText("\u9009\u62E9\u80CC\u666F\u989C\u8272");

		Button btnNewButton_3 = new Button(this, SWT.NONE);
		btnNewButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				set(tileType);
				imagePath.setText("");
				canvas.setImage(null);
			}
		});
		btnNewButton_3.setBounds(96, 235, 80, 27);
		btnNewButton_3.setText("\u53D6\u6D88\u56FE\u7247");
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u683C\u5B50\u7C7B\u578B\u7F16\u8F91\u5668");
		setSize(450, 300);

	}

	private void set(TileType tileType) {
		text.setText(tileType.name != null ? tileType.name : "");
		imagePath.setText(tileType.icon != null ? tileType.icon : "");
		canvas.setImage(tileType.icon);
		canvas.setBackground(ResourceManageX.getColor(tileType.color));
		color = tileType.color;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
